<?php
/* 
 * Information about a Product
 */

class Product {
  private $productId;
  private $category;
  private $description;
  private $details;
  private $price;
  private $imageUrl;
  private $thumbImageUrl;
  private $imageCaption;
  private $created;
  private $lastModified;
  private $createdBy;
  private $lastModifiedBy;

  function __construct($productId=null, $category=null, $description=null, $details=null, $price=null, $imageUrl=null, $thumbImageUrl=null, $imageCaption=null, $created=null, $lastModified=null, $createdBy=null, $lastModifiedBy=null) {
    $this->productId = $productId;
    $this->category = $category;
    $this->description = $description;
    $this->details = $details;
    $this->price = $price;
    $this->imageUrl = $imageUrl;
    $this->thumbImageUrl = $thumbImageUrl;
    $this->imageCaption = $imageCaption;
    $this->created = $created;
    $this->lastModified = $lastModified;
    $this->createdBy = $createdBy;
    $this->lastModifiedBy = $lastModifiedBy;
  }


  public function getProductId() {
    return $this->productId;
  }

  public function setProductId($productId) {
    $this->productId = $productId;
  }

  public function getCategory() {
    return $this->category;
  }

  public function setCategory($category) {
    $this->category = $category;
  }

  public function getDescription() {
    return $this->description;
  }

  public function setDescription($description) {
    $this->description = $description;
  }

  public function getDetails() {
    return $this->details;
  }

  public function setDetails($details) {
    $this->details = $details;
  }

  public function getPrice() {
    return $this->price;
  }

  public function setPrice($price) {
    $this->price = $price;
  }

  public function getImageUrl() {
    return $this->imageUrl;
  }

  public function setImageUrl($imageUrl) {
    $this->imageUrl = $imageUrl;
  }

  public function getThumbImageUrl() {
    return $this->thumbImageUrl;
  }

  public function setThumbImageUrl($thumbImageUrl) {
    $this->thumbImageUrl = $thumbImageUrl;
  }

  public function getImageCaption() {
    return $this->imageCaption;
  }

  public function setImageCaption($imageCaption) {
    $this->imageCaption = $imageCaption;
  }

  public function getCreated() {
    return $this->created;
  }

  public function setCreated($created) {
    $this->created = $created;
  }

  public function getLastModified() {
    return $this->lastModified;
  }

  public function setLastModified($lastModified) {
    $this->lastModified = $lastModified;
  }

  public function getCreatedBy() {
    return $this->createdBy;
  }

  public function setCreatedBy($createdBy) {
    $this->createdBy = $createdBy;
  }

  public function getLastModifiedBy() {
    return $this->lastModifiedBy;
  }

  public function setLastModifiedBy($lastModifiedBy) {
    $this->lastModifiedBy = $lastModifiedBy;
  }

public static function getProduct($productId) {
  $retVal = null;
  if(!empty($productId) && is_integer((int)$productId)){
    $sql = "Select ProductId, a.CategoryId, b.Name as CategoryName, a.Description, Details, Price, ImageUrl, ThumbImageUrl, ImageCaption from Product a, Category b where a.CategoryId=b.CategoryId and ProductId=".$productId;
    $product = self::loadProducts(Utilities::selectQuery($sql));
    if (sizeof($product)===1) {
      $retVal = $product[0];
    }
  }
  return $retVal;
}

public static function getAllProducts($limit=25) {
  $sql = "Select ProductId, a.CategoryId, b.Name as CategoryName, a.Description, Details, Price, ImageUrl, ThumbImageUrl, ImageCaption from Product a, Category b where a.CategoryId=b.CategoryId LIMIT ".$limit;
  $result = Utilities::selectQuery($sql);
  return self::loadProducts($result);
}

public static function getProductsForCategory($categoryId) {
  $retVal = null;
  if(!empty($categoryId) && is_integer((int)$categoryId)){
    $sql = "Select ProductId, a.CategoryId, b.Name as CategoryName, a.Description, Details, Price, ImageUrl, ThumbImageUrl, ImageCaption from Product a, Category b where a.CategoryId=b.CategoryId and b.CategoryId=".$categoryId;
    $retVal = self::loadProducts(Utilities::selectQuery($sql));
  }
  return $retVal;
}

private static function loadProducts ($result) {
  $retVal = null;
  if(!empty($result)){
    if (sizeof($result)>0){
      $retVal = array();
      foreach($result as $product){
        array_push($retVal, new Product($product["ProductId"], $product["CategoryName"],$product["Description"], $product["Details"],$product["Price"], $product["ImageUrl"],$product["ThumbImageUrl"],$product["ImageCaption"]));
      }
    }
  }
  return $retVal;
}

}

?>
